set more off
*Establish your own path to the following file, also available on the website where you got this code.
use "G:\My Drive\Media Systems\oecd.dta", clear 
save "G:\My Drive\Media Systems\Politics as Usual\oecd_uk.dta", replace
rename daten date
drop if uk_cs==.
tsset date
tsfill
ipolate uk_cs date, gen (uk_cs_ipo) epolate
gen uk_cs_ipo_2=uk_cs_ipo[_n-30]
save "G:\My Drive\Media Systems\Politics as Usual\oecd_uk.dta", replace

clear
*Establish your own path to the following file, also available on the website where you got this code.
insheet using "G:\My Drive\Media Systems\Politics as Usual\media.systems.day.summariesJan2018.csv", comma
set more off
drop if paper!="FT"
drop if year=="1979"
destring day, replace
destring month, replace
destring year, replace
gen daten=mdy(month,day, year)
gen august=1 if month==8
replace august=0 if august==.

replace macposavs="." if macposavs=="NA"
destring macposavs, replace

replace macnegavs="." if macnegavs=="NA"
destring macnegavs, replace

replace macneuavs="." if macneuavs=="NA"
destring macneuavs, replace

replace micposavs="." if micposavs=="NA"
destring micposavs, replace

replace micnegavs="." if micnegavs=="NA"
destring micnegavs, replace

replace micneuavs="." if micneuavs=="NA"
destring micneuavs, replace

replace pcpposavs="." if pcpposavs=="NA"
destring pcpposavs, replace

replace pcpnegavs="." if pcpnegavs=="NA"
destring pcpnegavs, replace

replace pcpneuavs="." if pcpneuavs=="NA"
destring pcpneuavs, replace

replace opyposavs="." if opyposavs=="NA"
destring opyposavs, replace

replace opynegavs="." if opynegavs=="NA"
destring opynegavs, replace

replace opyneuavs="." if opyneuavs=="NA"
destring opyneuavs, replace

replace othposavs="." if othposavs=="NA"
destring othposavs, replace

replace othnegavs="." if othnegavs=="NA"
destring othnegavs, replace

replace othneuavs="." if othneuavs=="NA"
destring othneuavs, replace

* go from pub day to pub day, not calendar day, which wd involve interpolation
gen trend=_n


* dfuller - Null is unit root.  

tsset trend
dfuller macroavs, trend
dfuller microavs, trend
dfuller pcompavs, trend
dfuller opolyavs, trend
dfuller positavs, trend
dfuller negatavs, trend
dfuller neutravs, trend
dfuller macposavs, trend
dfuller macnegavs, trend
dfuller macneuavs, trend
dfuller pcpposavs, trend
dfuller pcpnegavs, trend
dfuller pcpneuavs, trend
dfuller opyposavs, trend
dfuller opynegavs, trend
dfuller othposavs, trend
dfuller othnegavs, trend
dfuller othneuavs, trend

* all fine

* count since 1979 election
gen uk_elec_count=daten-7063
replace uk_elec_count=daten-8557 if daten>8556
replace uk_elec_count=daten-10023 if daten>10022
replace uk_elec_count=daten-11787 if daten>11786
replace uk_elec_count=daten-13635 if daten>13634
replace uk_elec_count=daten-15133 if daten>15132
replace uk_elec_count=daten-16561 if daten>16561
replace uk_elec_count=daten-18388 if daten>18388


* from dissolution of parliament to election [mean over 7 elecs is almost 26 days]
gen campaign=0
replace campaign=1 if daten>8532 & daten<8557
replace campaign=1 if daten>9998 & daten<10023
replace campaign=1 if daten>11757 & daten<11787
replace campaign=1 if daten>13611 & daten<13635
replace campaign=1 if daten>15105 & daten<15133
replace campaign=1 if daten>16536 & daten<16562
replace campaign=1 if daten>18363 & daten<18389

*campaign1 elecdate+26
gen campaign1=0
replace campaign1=1 if daten>8532 & daten<8558
replace campaign1=1 if daten>9998 & daten<10024
replace campaign1=1 if daten>11757 & daten<11783
replace campaign1=1 if daten>13611 & daten<13637
replace campaign1=1 if daten>15105 & daten<15131
replace campaign1=1 if daten>16536 & daten<16562
replace campaign1=1 if daten>18363 & daten<18389

*campaign2 elecdate+15
gen campaign2=0
replace campaign2=1 if daten>8532 & daten<8547
replace campaign2=1 if daten>9998 & daten<10013
replace campaign2=1 if daten>11757 & daten<11772
replace campaign2=1 if daten>13611 & daten<13626
replace campaign2=1 if daten>15105 & daten<15120
replace campaign2=1 if daten>16536 & daten<16551
replace campaign2=1 if daten>18363 & daten<18378

* post-election
gen postelec=0
replace postelec=1 if daten>8557 & daten<8587
replace postelec=1 if daten>10023 & daten<10053
replace postelec=1 if daten>11787 & daten<11817
replace postelec=1 if daten>13635 & daten<13665
replace postelec=1 if daten>15133 & daten<15163
replace postelec=1 if daten>16562 & daten<16592
replace postelec=1 if daten>18389 & daten<18419

gen uk_budg_count=daten-7102 if daten>7101
replace uk_budg_count=daten-7390  if daten>7389
replace uk_budg_count=daten-7739  if daten>7738
replace uk_budg_count=daten-8103 if daten>8102
replace uk_budg_count=daten-8474 if daten>8473
replace uk_budg_count=daten-8838 if daten>8837
replace uk_budg_count=daten-9209 if daten>9208
replace uk_budg_count=daten-9573 if daten>9572
replace uk_budg_count=daten-9937 if daten>9936
replace uk_budg_count=daten-10301 if daten>10300
replace uk_budg_count=daten-10665 if daten>10664
replace uk_budg_count=daten-11036 if daten>11035
replace uk_budg_count=daten-11400 if daten>11399
replace uk_budg_count=daten-11757 if daten>11756
replace uk_budg_count=daten-12128 if daten>12127
replace uk_budg_count=daten-12387 if daten>12386
replace uk_budg_count=daten-12751 if daten>12750
replace uk_budg_count=daten-13115 if daten>13114
replace uk_budg_count=daten-13479 if daten>13478
replace uk_budg_count=daten-13697 if daten>13696
replace uk_budg_count=daten-13955 if daten>13954
replace uk_budg_count=daten-14312 if daten>14311
replace uk_budg_count=daten-14690 if daten>14689
replace uk_budg_count=daten-15041 if daten>15040
replace uk_budg_count=daten-15447 if daten>15446
replace uk_budg_count=daten-15804 if daten>15803
replace uk_budg_count=daten-16147 if daten>16146
replace uk_budg_count=daten-16511 if daten>16510
replace uk_budg_count=daten-16882 if daten>16881
replace uk_budg_count=daten-17246 if daten>17245
replace uk_budg_count=daten-17603 if daten>17602
replace uk_budg_count=daten-18009 if daten>18008
replace uk_budg_count=daten-18345 if daten>18344
replace uk_budg_count=daten-18709 if daten>18708
replace uk_budg_count=daten-19073 if daten>19072

gen postbudg=1 if date>7102 & date<7133
replace postbudg=1 if date>7390	& date<7421
replace postbudg=1 if date>7739	&  date<7770
replace postbudg=1 if date>8103	&  date<8134
replace postbudg=1 if date>8474	&  date<8505
replace postbudg=1 if date>8838	&  date<8869
replace postbudg=1 if date>9209	&  date<9240
replace postbudg=1 if date>9573	&  date<9604
replace postbudg=1 if date>9937	&  date<9968
replace postbudg=1 if date>10301	&  date<10332
replace postbudg=1 if date>10665	&  date<10696
replace postbudg=1 if date>11036	&  date<11067
replace postbudg=1 if date>11400	&  date<11431
replace postbudg=1 if date>11757	&  date<11788
replace postbudg=1 if date>12128	&  date<12159
replace postbudg=1 if date>12387	&  date<12418
replace postbudg=1 if date>12751	&  date<12782
replace postbudg=1 if date>13115	&  date<13146
replace postbudg=1 if date>13479	&  date<13510
replace postbudg=1 if date>13697	&  date<13728
replace postbudg=1 if date>13955	&  date<13986
replace postbudg=1 if date>14312	&  date<14343
replace postbudg=1 if date>14690	&  date<14721
replace postbudg=1 if date>15041	&  date<15072
replace postbudg=1 if date>15447	&  date<15478
replace postbudg=1 if date>15804	&  date<15835
replace postbudg=1 if date>16147	&  date<16178
replace postbudg=1 if date>16511	&  date<16542
replace postbudg=1 if date>16882	&  date<16913
replace postbudg=1 if date>17246	&  date<17277
replace postbudg=1 if date>17603	&  date<17634
replace postbudg=1 if date>18009	&  date<18040
replace postbudg=1 if date>18345	&  date<18376
replace postbudg=1 if date>18709	&  date<18740
replace postbudg=1 if date>19073	&  date<19104
replace postbudg=0 if postbudg==.


gen prebudget=1 if daten>7072 & daten<7103
replace prebudget=1 if daten>7360 & daten<7391
replace prebudget=1 if daten>7709 & daten<7740 
replace prebudget=1 if daten>8073 & daten<8104
replace prebudget=1 if daten>8444 & daten<8475
replace prebudget=1 if daten>8808 & daten<8839
replace prebudget=1 if daten>9179 & daten<9210
replace prebudget=1 if daten>9543 & daten<9574
replace prebudget=1 if daten>9907 & daten<9938
replace prebudget=1 if daten>10271 & daten<10302
replace prebudget=1 if daten>10635	& daten<10666
replace prebudget=1 if daten>11006	& daten<11037
replace prebudget=1 if daten>11370	& daten<11401
replace prebudget=1 if daten>11727	& daten<11758
replace prebudget=1 if daten>12098	& daten<12129
replace prebudget=1 if daten>12357	& daten<12388
replace prebudget=1 if daten>12721	& daten<12752
replace prebudget=1 if daten>13085	& daten<13116
replace prebudget=1 if daten>13449	& daten<13480
replace prebudget=1 if daten>13667	& daten<13698
replace prebudget=1 if daten>13925	& daten<13956
replace prebudget=1 if daten>14282	& daten<14313
replace prebudget=1 if daten>14660	& daten<14691
replace prebudget=1 if daten>15011	& daten<15042
replace prebudget=1 if daten>15417	& daten<15448
replace prebudget=1 if daten>15774	& daten<15805
replace prebudget=1 if daten>16117	& daten<16148
replace prebudget=1 if daten>16481	& daten<16512
replace prebudget=1 if daten>16852	& daten<16883
replace prebudget=1 if daten>17216	& daten<17247
replace prebudget=1 if daten>17573	& daten<17604
replace prebudget=1 if daten>17979	& daten<18010
replace prebudget=1 if daten>18315	& daten<18346
replace prebudget=1 if daten>18679	& daten<18710
replace prebudget=1 if daten>19043	& daten<19074
replace prebudget=0 if prebudget==.

rename daten date
merge m:m date using "G:\My Drive\Media Systems\Politics as Usual\oecd_uk.dta", keepusing(uk_cs_ipo_2)
drop if date==.
duplicates drop
drop in 1
bysort date: egen macrod=mean(macroavs)
bysort date: egen microd=mean(microavs)
bysort date: egen pcompd=mean(pcompavs)
bysort date: egen opolyd=mean(opolyavs)
bysort date: egen posd=mean(positavs)
bysort date: egen negd=mean(negatavs)
bysort date: egen neutrd=mean(neutravs)



drop macroavs microavs pcompavs opolyavs positavs negatavs neutravs alldaypars macrodaypars microdaypars pcompdaypars opolydaypars otherdaypars macposavs macnegavs macneuavs micposavs micnegavs micneuavs pcpposavs pcpnegavs pcpneuavs opyposavs opynegavs opyneuavs othposavs othnegavs othneuavs

duplicates drop date, force


tsset date

dfuller uk_cs_ipo_2, trend

drop if _merge!=3
replace trend=_n
tsset trend

log using "G:\My Drive\Media Systems\Politics as Usual\Usual_arima.smcl", replace

summarize macrod microd opolyd pcompd
*macro 0.352, micro 0.398, opoly 0.426, polcomp 0.239

gen logit_macrod=logit(macrod)
gen logit_pcompd=logit(pcompd)

dfuller logit_macrod, trend
dfuller logit_pcompd, trend

ac logit_macrod
pac logit_macrod

arima logit_macrod, ar (1) ma (1)
predict resid, residuals
ac resid
pac resid
drop resid

* The range for ac and pac for this model is -.04 to .06, a very small correlation.  The range for the con interval is a truly tiny -.02 to 02.
* Presumably this is because of the large N and maybe because these are daily averages too.  
* We need to go to roughly ar (1 2 3 4 5 6) and ma (1 2 3 4 5 6) to get the vast majority of ac and pac within the intervals.
* However, this model comes up agains a flat log likelihood when indo variables are added.

ac logit_pcompd
pac logit_pcompd
arima logit_macrod, ar (1) ma (1)
predict resid, residuals
ac resid
pac resid
drop resid

*Same as above

gen uk_elec_count_sq=uk_elec_count*uk_elec_count
dfuller uk_elec_count_sq, trend
gen campaign_cs=campaign1*ld.uk_cs_ipo_2
dfuller campaign_cs, trend

set more off

tsset date
generate monthly=mofd(date)
drop _merge
*Establish your own path to the following file, also available on the website where you got this code.
merge m:m monthly using "G:\My Drive\Media Systems\Jnlism\Resub\ICM.dta", keepusing(CONLEADOVERLABOURpercentage)
gen conlead=abs(CONLEADOVERLABOURpercentage)
drop if macro==.
gen campaign_lead=campaign1*conlead
* mean abs diff is 0.09 min 0, max 0.39
gen conlead_sq=conlead*conlead
gen close=1 if conlead<0.03
replace close=0 if close==.

tsset trend
sort trend
arima logit_macrod campaign1 campaign_cs uk_elec_count uk_elec_count_sq ld.uk_cs_ipo_2 august ,  ar (1) ma (1) vce(robust)
* Untransformed DV has same signs and similar relative magnitudes
* Doing an invlogit on the coefficients changes the signs and is generally unconvincing
* cannot compare to untransformed version for polcomp, due to flat log likelihood


predict resid, residuals
wntestb resid, table
wntestq resid
* both of these tests suggest the residuals are not white noise
* but as explained above, we have traded off a little bit of ac and pac, for the ability to test our model.
* also it's a gigantic reduction in the test statistic versus the dv itself
gen residsq=resid*resid
wntestq residsq
wntestb residsq, table
* these seem fine, not sure if testing squared resids is equivalent to test for arch or something else entirely
drop resid residsq

estat ic

* The following transforms the coefficients back from logit
gen b_campaign1_macro=1-(exp(_b[campaign1]))
gen b_campaign_cs_macro=1-(exp(_b[campaign_cs]))
gen b_uk_elec_count_macro=1-(exp(_b[uk_elec_count]))
gen b_uk_elec_count_sq_macro=1-(exp(_b[uk_elec_count_sq]))
gen b_lduk_cs_ipo_2_macro=1-(exp(_b[ld.uk_cs_ipo_2]))
gen b_august_macro=1-(exp(_b[august]))
summarize  b_campaign1_macro b_campaign_cs_macro b_uk_elec_count_macro b_uk_elec_count_sq b_lduk_cs_ipo_2_macro b_august_macro

* factor variables not allowed

margins, dydx(campaign1)
 
margins,  at(ld.uk_cs_ipo_2=(-0.025(0.01)0.025) campaign1=(0 1)) vsquish
marginsplot, title("UK: Effect Consumer Sentiment and Campaign on Macroeconomic Policy Coverage", size(small)) xtitle("Consumer Sentiment") ytitle("Macroeconomic Policy Coverage")  note("Effect is adjusted prediction.", size(vsmall)) 
graph save "G:\My Drive\Media Systems\Draft\Graphs\UK_Macro_Election_Days_campaign_CS.gph", replace

margins, at(uk_elec_count=(50(50)1800)) vsquish
marginsplot
graph save "G:\My Drive\Media Systems\Draft\Graphs\UK_Macro_Election_Days.gph", replace

margins, at(uk_elec_count_sq=(50(50)1800)) vsquish
marginsplot
graph save "G:\My Drive\Media Systems\Draft\Graphs\UK_Macro_Election_Days_sq.gph", replace

clear matrix

set more off
arima logit_pcompd campaign1 campaign_cs uk_elec_count uk_elec_count_sq ld.uk_cs_ipo_2 august ,  ar (1) ma (1) vce(robust)


predict resid, residuals
wntestb resid, table
wntestq resid
* both of these tests suggest the residuals are not white noise
* but as explained above, we have traded off a little bit of ac and pac, for the ability to test our model.
* also it's a gigantic reduction in the test statistic versus the dv itself
gen residsq=resid*resid
wntestq residsq
wntestb residsq, table
* these seem fine, not sure if testing squared resids is equivalent to test for arch or something else entirely
drop resid residsq

estat ic

* SD of polcomp in UK is .033

* The following transforms the coefficients back from logit
gen b_campaign1_pcom=1-(exp(_b[campaign1]))
gen b_campaign_cs_pcom=1-(exp(_b[campaign_cs]))
gen b_uk_elec_count_pcom=1-(exp(_b[uk_elec_count]))
gen b_uk_elec_count_sq_pcom=1-(exp(_b[uk_elec_count_sq]))
gen b_lduk_cs_ipo_2_pcom=1-(exp(_b[ld.uk_cs_ipo_2]))
gen b_august_pcom=1-(exp(_b[august]))
summarize  b_campaign1_pcom b_campaign_cs_pcom b_uk_elec_count_pcom b_uk_elec_count_sq_pcom b_lduk_cs_ipo_2_pcom b_august_pcom

margins, dydx(campaign1)
 
margins,  at(ld.uk_cs_ipo_2=(-0.025(0.01)0.025) campaign1=(0 1)) vsquish
marginsplot, title("UK: Effect Consumer Sentiment and Campaign on Political Competition Coverage", size(small)) xtitle("Consumer Sentiment") ytitle("Political Competition Coverage")  note("Effect is adjusted prediction.", size(vsmall)) 
graph save "G:\My Drive\Media Systems\Draft\Graphs\UK_PolComp_Election_Days_campaign_CS.gph", replace

margins, at(uk_elec_count=(50(50)1800)) vsquish
marginsplot
graph save "G:\My Drive\Media Systems\Draft\Graphs\UK_PolComp_Election_Days.gph", replace

margins, at(uk_elec_count_sq=(50(50)1800)) vsquish
marginsplot
graph save "G:\My Drive\Media Systems\Draft\Graphs\UK_PolComp_Election_Days_sq.gph", replace

tsset trend
sort trend
arima logit_pcompd campaign1 campaign_cs uk_elec_count uk_elec_count_sq ld.uk_cs_ipo_2 august conlead ,  ar (1) ma (1) vce(robust)
* bigger the lead the more polcomp, insig
arima logit_pcompd campaign1 campaign_cs uk_elec_count uk_elec_count_sq ld.uk_cs_ipo_2 august conlead campaign_lead ,  ar (1) ma (1) vce(robust)
* not much diff
arima logit_macrod campaign1 campaign_cs uk_elec_count uk_elec_count_sq ld.uk_cs_ipo_2 august conlead ,  ar (1) ma (1) vce(robust)
* bigger the lead the less macro small and insig
arima logit_macrod campaign1 campaign_cs uk_elec_count uk_elec_count_sq ld.uk_cs_ipo_2 august conlead campaign_lead ,  ar (1) ma (1) vce(robust)
* not much diff

arima logit_macrod campaign1 campaign_cs uk_elec_count uk_elec_count_sq ld.uk_cs_ipo_2 august conlead conlead_sq ,  ar (1) ma (1) vce(robust)
* v similar to original


arima logit_macrod campaign1 campaign_cs uk_elec_count uk_elec_count_sq ld.uk_cs_ipo_2 august conlead conlead_sq ,  ar (1) ma (1) vce(robust)
* v similar to original


*Reformat date to label x axes more easily, label lines too

gen date2=date
format date2 %td


*1983
lowess macrod date if date>8416 & date<8556, bwidth(0.2) nograph generate (macrod_elec1date)
lowess pcompd date if date>8416 & date<8556, bwidth(0.2) nograph generate (pcompd_elec1date)
. twoway (line macrod_elec1date date2 if date>8416 & date<8556) (line pcompd_elec1date date2 if date>8416 & date<8556), yscale (range(0.2 0.45)) ylabel (0.2 (0.05) 0.45) xlabel(, angle(45)) scheme (s2mono) legend(order(1 "Macroeconomic Policy" 2 "Political Competition") cols(1) size(small)) xtitle("") title("UK 1983")
graph save uk_1983, replace

drop macrod_elec1date pcompd_elec1date
*1987
lowess macrod date if date>9874 & date<10009, bwidth(0.2) nograph generate (macrod_elec1date)
lowess pcompd date if date>9874 & date<10009, bwidth(0.2) nograph generate (pcompd_elec1date)
. twoway (line macrod_elec1date date2 if date>9874 & date<10009) (line pcompd_elec1date date2 if date>9874 & date<10009), yscale (range(0.2 0.45)) ylabel (0.2 (0.05) 0.45) xlabel(, angle(45)) scheme (s2mono) legend(order(1 "Macroeconomic Policy" 2 "Political Competition") cols(1) size(small)) xtitle("") title("UK 1987")
graph save uk_1987, replace

*1992
lowess macrod date if date>11638 & date<11788, bwidth(0.2) nograph generate (macrod_elec2date)
lowess pcompd date if date>11638 & date<11788, bwidth(0.2) nograph generate (pcompd_elec2date)
. twoway (line macrod_elec2date date2 if date>11638 & date<11788) (line pcompd_elec2date date2 if date>11638 & date<11788), yscale (range(0.2 0.45)) ylabel (0.2 (0.05) 0.45) xlabel(, angle(45)) scheme (s2mono) legend(order(1 "Macroeconomic Policy" 2 "Political Competition") cols(1) size(small)) xtitle("") title("UK 1992")
graph save uk_1992, replace

*1997
lowess macrod date if date>13486 & date<13636, bwidth(0.2) nograph generate (macrod_elec3date)
lowess pcompd date if date>13486 & date<13636, bwidth(0.2) nograph generate (pcompd_elec3date)
. twoway (line macrod_elec3date date2 if date>13486 & date<13636) (line pcompd_elec3date date2 if date>13486 & date<13636), yscale (range(0.2 0.45)) ylabel (0.2 (0.05) 0.45) xlabel(, angle(45)) scheme (s2mono) legend(order(1 "Macroeconomic Policy" 2 "Political Competition") cols(1) size(small)) xtitle("") title("UK 1997")
graph save uk_1997, replace

*2001
lowess macrod date if date>14984 & date<15134, bwidth(0.2) nograph generate (macrod_elec4date)
lowess pcompd date if date>14984 & date<15134, bwidth(0.2) nograph generate (pcompd_elec4date)
. twoway (line macrod_elec4date date2 if date>14984 & date<15134) (line pcompd_elec4date date2 if date>14984 & date<15134), yscale (range(0.2 0.45)) ylabel (0.2 (0.05) 0.45) xlabel(, angle(45)) scheme (s2mono) legend(order(1 "Macroeconomic Policy" 2 "Political Competition") cols(1) size(small)) xtitle("") title("UK 2001")
graph save uk_2001, replace

*2005
lowess macrod date if date>16412 & date<16562, bwidth(0.2) nograph generate (macrod_elec5date)
lowess pcompd date if date>16412 & date<16562, bwidth(0.2) nograph generate (pcompd_elec5date)
. twoway (line macrod_elec5date date2 if date>16412 & date<16562) (line pcompd_elec5date date2 if date>16412 & date<16562), yscale (range(0.2 0.45)) ylabel (0.2 (0.05) 0.45) xlabel(, angle(45)) scheme (s2mono) legend(order(1 "Macroeconomic Policy" 2 "Political Competition") cols(1) size(small)) xtitle("") title("UK 2005")
graph save uk_2005, replace

*2010
lowess macrod date if date>18239 & date<18389, bwidth(0.2) nograph generate (macrod_elec6date)
lowess pcompd date if date>18239 & date<18389, bwidth(0.2) nograph generate (pcompd_elec6date)
. twoway (line macrod_elec6date date2 if date>18250 & date<18389) (line pcompd_elec6date date2 if date>18250 & date<18389), yscale (range(0.2 0.45)) ylabel (0.2 (0.05) 0.45) xlabel(, angle(45)) scheme (s2mono) legend(order(1 "Macroeconomic Policy" 2 "Political Competition") cols(1) size(small)) xtitle("") title("UK 2010")
graph save uk_2010, replace

* install grc1leg
* net describe grc1leg, from(http://www.stata.com/users/vwiggins)

grc1leg uk_1983.gph uk_1987.gph uk_1992.gph uk_1997.gph uk_2001.gph uk_2005.gph uk_2010.gph
clear matrix

* I adjusted the graph manually

log off

set more off
*Establish your own path to the following file, also available on the website where you got this code.
use "G:\My Drive\Media Systems\oecd.dta", clear 
save "G:\My Drive\Media Systems\Politics as Usual\oecd_ger.dta", replace
rename daten date
drop if ger_cs==.
tsset date
tsfill
ipolate ger_cs date, gen (ger_cs_ipo) epolate
gen ger_cs_ipo_2=ger_cs_ipo[_n-30]
save "G:\My Drive\Media Systems\Politics as Usual\oecd_ger.dta", replace

use "G:\My Drive\Media Systems\New Data\DW.dta", clear

gen trend=_n

* count since 1998 election
gen ger_elec_count=date-14149
replace ger_elec_count=date-15605 if date>15604
replace ger_elec_count=date-16697 if date>16696
replace ger_elec_count=date-18168 if date>18167

* no official campaign period in Germany
* campaign 1 elec +26
gen campaign1=0
replace campaign1=1 if date>15574 & date<15600
replace campaign1=1 if  date>16666 & date<16692
replace campaign1=1 if date>18137 & date<18163

* campaign 2 elec +15
gen campaign2=0
replace campaign2=1 if date>15574 & date<15589
replace campaign2=1 if  date>16666 & date<16681
replace campaign2=1 if date>18137 & date<18152


* postelection
gen postelec=0
replace postelec=1 if date>15606 & date<15636
replace postelec=1 if  date>16698 & date<16728
replace postelec=1 if date>18168 & date<18198


* count since budgets
gen ger_budg_count=date-14502
replace ger_budg_count=date-14865
replace ger_budg_count=date-15229 if date>15228
replace ger_budg_count=date-15595 if date>15594
replace ger_budg_count=date-15957 if date>15956
replace ger_budg_count=date-16321 if date>16320
replace ger_budg_count=date-16888 if date>16887
replace ger_budg_count=date-17049 if date>17048
replace ger_budg_count=date-17420 if date>17419
replace ger_budg_count=date-17791 if date>17990
replace ger_budg_count=date-18281 if date>18280
replace ger_budg_count=date-18876 if date>18875
replace ger_budg_count=date-19247 if date>19246

gen postbudg=1 if date>14502 & date<14533
replace postbudg=1 if date>14865	& date<14896
replace postbudg=1 if date>15229	& date<115260
replace postbudg=1 if date>15595	& date<115626
replace postbudg=1 if date>15957	& date<115988
replace postbudg=1 if date>16321	& date<116352
replace postbudg=1 if date>16888	& date<116919
replace postbudg=1 if date>17049	& date<117080
replace postbudg=1 if date>17420	& date<117451
replace postbudg=1 if date>17791	& date<117822
replace postbudg=1 if date>18281	& date<118312
replace postbudg=1 if date>18876	& date<118907
replace postbudg=1 if date>19247	& date<119278
replace postbudg=0 if postbudg==.




* German budgets
gen prebudget=1 if date>14472 & date<14503
replace prebudget=1 if date>14835	& date<14866
replace prebudget=1 if date>15199	& date<15230
replace prebudget=1 if date>15565	& date<15596
replace prebudget=1 if date>15927	& date<15958
replace prebudget=1 if date>16291	& date<16322
replace prebudget=1 if date>16858	& date<16889
replace prebudget=1 if date>17019	& date<17050
replace prebudget=1 if date>17390	& date<17421
replace prebudget=1 if date>17761	& date<17792
replace prebudget=1 if date>18251	& date<18282
replace prebudget=1 if date>18846	& date<18877
replace prebudget=1 if date>19217	& date<19248
replace prebudget=0 if prebudget==.

*Establish your own path to the following file, also available on the website where you got this code.
merge m:m date using "G:\My Drive\Media Systems\Politics as Usual\oecd_ger.dta", keepusing(ger_cs_ipo_2)
drop if date==.
duplicates drop
drop in 1
tsset date
gen month=month(date)
gen august=1 if month==8
replace august=0 if august==.

dfuller ger_cs_ipo_2, trend

drop if _merge!=3
replace trend=_n
tsset trend


log on

summarize macrod microd otherpold polcompd
* macro 0.259, micro 0.313, otherpol 0.418, polcompd 0.267


gen logit_macrod=logit(macrod)
gen logit_polcompd=logit(polcompd)

dfuller logit_macrod, trend
dfuller logit_polcompd, trend

ac logit_macrod
pac logit_macrod


arima logit_macrod, ar (1 6) ma (1 6)
predict resid, residuals
ac resid
pac resid
drop resid

* The range for ac and pac for this model is -.05 to .12, a small correlation.
* It has a theoretical meaning too.  The coverage is seasonal (weekly), Die Welt being published from Monday to Saturday.
* Ar 123 Ma 123 gets the ac and pac into a range similar to the UK -.05 to .05, but no se for MA L1 and L2 on polcompd and 786 iterations.
* Ar 1 ma 1 and ar 1 2 ma 1 2 have quite large correlations in the range -.1 to .18
* no time series the range is 0 to 0.5, so there's a big reduction from that and from 1 and from 1 2.


gen ger_elec_count_sq=ger_elec_count*ger_elec_count
dfuller ger_elec_count_sq, trend
gen campaign_cs=campaign1*ld.ger_cs_ipo_2
dfuller campaign_cs, trend


generate monthly=mofd(date)
drop _merge
*Establish your own path to the following file, also available on the website where you got this code.
merge m:m monthly using "G:\My Drive\Media Systems\Jnlism\Resub\Ger_polls.dta"
drop if macro==.
gen comp=abs(conlead_m)
gen campaign_lead=campaign1*conlead_m
gen close=1 if conlead_m<0.03
replace close=0 if close==.
gen conlead_sq=conlead_m*conlead_m


set more off
sort trend
arima logit_macrod campaign1 campaign_cs ger_elec_count ger_elec_count_sq ld.ger_cs_ipo_2 august ,  ar (1 6) ma (1 6) vce(robust)

predict resid, residuals
wntestb resid, table
wntestq resid
* both of these tests suggest the residuals are not white noise
* but as explained above, we have traded off a little bit of ac and pac, for the ability to test our model.
* also it's a gigantic reduction in the test statistic versus the dv itself
gen residsq=resid*resid
wntestq residsq
wntestb residsq, table
* these seem fine, not sure if testing squared resids is equivalent to test for arch or something else entirely
drop resid residsq

estat ic


* The following transforms the coefficients back from logit
gen b_campaign1_macro=1-(exp(_b[campaign1]))
gen b_campaign_cs_macro=1-(exp(_b[campaign_cs]))
gen b_ger_elec_count_macro=1-(exp(_b[ger_elec_count]))
gen b_ger_elec_count_sq_macro=1-(exp(_b[ger_elec_count_sq]))
gen b_ldger_cs_ipo_2_macro=1-(exp(_b[ld.ger_cs_ipo_2]))
gen b_august_macro=1-(exp(_b[august]))
summarize  b_campaign1_macro b_campaign_cs_macro b_ger_elec_count_macro b_ger_elec_count_sq b_ldger_cs_ipo_2_macro b_august_macro

margins, dydx(campaign1)
 
margins,  at(ld.ger_cs_ipo_2=(-0.025(0.01)0.025) campaign1=(0 1)) vsquish
marginsplot, title("Germany: Effect Consumer Sentiment and Campaign on Macroeconomic Policy Coverage", size(small)) xtitle("Consumer Sentiment") ytitle("Macroeconomic Policy Coverage")  note("Effect is adjusted prediction.", size(vsmall)) 
graph save "G:\My Drive\Media Systems\Draft\Graphs\Ger_Macro_Election_Days_campaign_CS.gph", replace

margins, at(ger_elec_count=(50(50)1800)) vsquish
marginsplot
graph save "G:\My Drive\Media Systems\Draft\Graphs\Ger_Macro_Election_Days.gph", replace

margins, at(ger_elec_count_sq=(50(50)1800)) vsquish
marginsplot
graph save "G:\My Drive\Media Systems\Draft\Graphs\Ger_Macro_Election_Days_sq.gph", replace


drop b_campaign1_macro b_campaign_cs_macro b_ger_elec_count_macro b_ger_elec_count_sq b_ldger_cs_ipo_2_macro b_august_macro
clear matrix
set more off

arima logit_polcompd campaign1 campaign_cs ger_elec_count ger_elec_count_sq ld.ger_cs_ipo_2 august ,  ar (1 6) ma (1 6) vce(robust)
* factor variables not allowed


predict resid, residuals
wntestb resid, table
wntestq resid
* both of these tests suggest the residuals are not white noise
* but as explained above, we have traded off a little bit of ac and pac, for the ability to test our model.
* also it's a gigantic reduction in the test statistic versus the dv itself
gen residsq=resid*resid
wntestq residsq
wntestb residsq, table
* these seem fine, not sure if testing squared resids is equivalent to test for arch or something else entirely
drop resid residsq

estat ic

* The following transforms the coefficients back from logit
gen b_campaign1_polcomp=1-(exp(_b[campaign1]))
gen b_campaign_cs_polcomp=1-(exp(_b[campaign_cs]))
gen b_ger_elec_count_polcomp=1-(exp(_b[ger_elec_count]))
gen b_ger_elec_count_sq_polcomp=1-(exp(_b[ger_elec_count_sq]))
gen b_ldger_cs_ipo_2_polcomp=1-(exp(_b[ld.ger_cs_ipo_2]))
gen b_august_polcomp=1-(exp(_b[august]))
summarize  b_campaign1_polcomp b_campaign_cs_polcomp b_ger_elec_count_polcomp b_ger_elec_count_sq b_ldger_cs_ipo_2_polcomp b_august_polcomp


* SD of polcompd in Germany is .043

margins, dydx(campaign1)
 
margins,  at(ld.ger_cs_ipo_2=(-0.025(0.01)0.025) campaign1=(0 1)) vsquish
marginsplot, title("Germany: Effect Consumer Sentiment and Campaign on Political Competition Coverage", size(small)) xtitle("Consumer Sentiment") ytitle("Political Competition Coverage")  note("Effect is adjusted prediction.", size(vsmall)) 
graph save "G:\My Drive\Media Systems\Draft\Graphs\Ger_PolComp_Election_Days_campaign_CS.gph", replace

margins, at(ger_elec_count=(50(50)1800)) vsquish
marginsplot
graph save "G:\My Drive\Media Systems\Draft\Graphs\Ger_PolComp_Election_Days.gph", replace

margins, at(ger_elec_count_sq=(50(50)1800)) vsquish
marginsplot
graph save "G:\My Drive\Media Systems\Draft\Graphs\Ger_PolComp_Election_Days_sq.gph", replace
clear matrix

sort trend
arima logit_polcompd campaign1 campaign_cs ger_elec_count ger_elec_count_sq ld.ger_cs_ipo_2 conlead_m august ,  ar (1 6) ma (1 6) vce(robust)
* bigger the lead the less polcomp (at 10%) -0.338
* switches campaign coef to neg (but insig)

arima logit_polcompd campaign1 campaign_cs ger_elec_count ger_elec_count_sq ld.ger_cs_ipo_2 conlead_m campaign_lead august ,  ar (1 6) ma (1 6) vce(robust)

* bigger the lead the less polcomp (at 10%) -0.359
* switches campaign coef to neg (but insig)
* not much diff to main effect

arima logit_macrod campaign1 campaign_cs ger_elec_count ger_elec_count_sq ld.ger_cs_ipo_2 conlead_m august ,  ar (1 6) ma (1 6) vce(robust)
* campaign coef 0.058 (still sig at 5%)
* con lead small(25% of polcompd)and stat insig reduction in macro 

arima logit_macrod campaign1 campaign_cs ger_elec_count ger_elec_count_sq ld.ger_cs_ipo_2 conlead_m campaign_lead august ,  ar (1 6) ma (1 6) vce(robust)
* campaign coeff smaller 0.035 and now insig
* not much diff to main effect otherwise

arima logit_polcompd campaign1 campaign_cs ger_elec_count ger_elec_count_sq ld.ger_cs_ipo_2 conlead_m conlead_sq august ,  ar (1 6) ma (1 6) vce(robust)
* v little diff

arima logit_macrod campaign1 campaign_cs ger_elec_count ger_elec_count_sq ld.ger_cs_ipo_2 conlead_m conlead_sq august ,  ar (1 6) ma (1 6) vce(robust)

* v little diff

*Germany
gen daten=date

*2002 23 sep
lowess macrod date if date>15456 & date<15606, bwidth(0.2) nograph generate (macrod_elec1date)
lowess polcompd date if date>15456 & date<15606, bwidth(0.2) nograph generate (polcompd_elec1date)
. twoway (line macrod_elec1date date if date>15456 & date<15606) (line polcompd_elec1date date if date>15456 & date<15606), yscale (range(0.2 0.45)) ylabel (0.2 (0.05) 0.45) xlabel(, angle(45)) scheme (s2mono) legend(order(1 "Macroeconomic Policy" 2 "Political Competition") cols(1) size(small)) xtitle("") title("Germany 2002") 
graph save ger_2002, replace

*2005 19 sep
lowess macrod date if date>16548 & date<16698, bwidth(0.2) nograph generate (macrod_elec2date)
lowess polcompd date if date>16548 & date<16698, bwidth(0.2) nograph generate (polcompd_elec2date)
. twoway (line macrod_elec2date date if date>16548 & date<16698) (line polcompd_elec2date date if date>16548 & date<16698), yscale (range(0.2 0.45)) ylabel (0.2 (0.05) 0.45) xlabel(, angle(45)) scheme (s2mono) legend(order(1 "Macroeconomic Policy" 2 "Political Competition") cols(1) size(small)) xtitle("") title("Germany 2005") 
graph save ger_2005, replace


*2009 29 sep
lowess macrod date if date>18019 & date<18169, bwidth(0.2) nograph generate (macrod_elec3date)
lowess polcompd date if date>18019 & date<18169, bwidth(0.2) nograph generate (polcompd_elec3date)
. twoway (line macrod_elec3date date if date>18019 & date<18169) (line polcompd_elec3date date if date>18019 & date<18169), yscale (range(0.2 0.45)) ylabel (0.2 (0.05) 0.45) xlabel(, angle(45)) scheme (s2mono) legend(order(1 "Macroeconomic Policy" 2 "Political Competition") cols(1) size(small)) xtitle("") title("Germany 2009") 
graph save ger_2009, replace



grc1leg uk_1987.gph uk_1992.gph uk_1997.gph uk_2001.gph uk_2005.gph uk_2010.gph 

grc1leg uk_2001.gph uk_2005.gph uk_2010.gph ger_2002.gph ger_2005.gph ger_2009.gph   
*then crop uk graphs

log close
